<?php
/**
 * 验证码生成的演示工具
 */

$type = $_GET['type'];
if(!in_array($type, array('brand', 'rela', 'rela2'))){
	$type = 'brand';
}
$arr = array(
	'brand' => '团购上拉手',
	'rela' => '大众点评团',
	'rela2' => '果冻凉鞋'
);


createImage($type, $arr[$type]);

function createImage($type, $adwords){
	$l = mb_strlen($adwords);
	$w = 45 + $l*7;
	$h = 35;
	
	$img = imagecreatetruecolor($w, $h);
	/**
	$checkcode = '百度搜索';
	$dir = dirname(__FILE__);
	//exit($dir);
	$res = imagecreatetruecolor(200, 40);
	$white = imagecolorallocate($res ,255, 255, 255);
	imagefilledrectangle($res, 0, 0, 200, 40, $white);
	
	foreach(str_split($checkcode, 3) as $key => $item){
		imagettftext($res, 14, rand(-10,10), 5 + $key * 14 + rand(-5,5), 20, imagecolorallocate($res, 0, 0, 255), $dir.'/msyh.ttf', $item);
	}
	*/
	imagefill($img, 0, 0, imagecolorallocate($img, 255, 255, 255));
	$str = randChar();
	$_SESSION['sec'] = implode('', $str);
	//随机字体
	if(is_file('song.ttc')){
		foreach($str as $key => $item){
			imagettftext($img, 14, mt_rand(-30,30), $l * 7 + 5 + $key * 14 + mt_rand(-5,5), 22, imagecolorallocate($img, 0, 0, 0), 'song.ttc', $item);
		}
	}else{
		exit('TTC Error!');
	}
	//添加噪点
	$color = imagecolorallocate($img, 0, 0, 0);
	for($i = 0; $i < 250; $i++){
		//imagesetpixel($img, $l * 7 + 5 + rand(0, 40), rand(0,30), $color);
		imagesetpixel($img, mt_rand(0, $l * 7 + 5 + 40), mt_rand(0,30), $color);
	}
	//添加噪线
	imageline($img, $l * 7 + 5, mt_rand(3, 27), $l * 7 + 5 + 40, mt_rand(3, 27), $color);
	//广告字体
	imagettftext($img, 14, 0, 10, 22, imagecolorallocate($img, 255, 0, 0), 'song.ttc', $adwords);
	header('Content-type: image/png');
	imagepng($img);
	imagedestroy($img);
}

function randChar($l = 2){
	$str = array();
	$string = array('你','好','中','关','村','难','人','上','下','天','地');
	for($i = 0; $i < $l; $i++){
		$str[] = $string[rand(0,10)];
	}
	return $str;
}
?>